﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Skymate.CacheDemo
{
    using System;
    using System.Diagnostics;

    class Program
    {
        private static int GetCacheRandom(ICache cache)
        {
            var cacheItem = cache.Get("random",
                (key) =>
                {
                    Random random = new Random();
                    return random.Next(1, 1000);
                });
            return int.Parse(cacheItem.ToString());
        }

        private static void ShowCacheItem(int value)
        {
            Console.WriteLine("{0}  取数：{1}",DateTime.Now.ToString("HH:mm:ss"), value);
        }

        static void Main(string[] args)
        {
            ICache cache = new DemoCache();

            var cacheItem = GetCacheRandom(cache);
            Console.Write("第一次取值    ");
            ShowCacheItem(cacheItem);

            Stopwatch watch=new Stopwatch();
            watch.Start();

            while (true)
            {
                if (watch.ElapsedMilliseconds < 10000)
                {
                    continue;
                }

                cacheItem = GetCacheRandom(cache);
                Console.Write("10s后取值    ");
                ShowCacheItem(cacheItem);
                break;
            }

            while (true)
            {
                if (watch.ElapsedMilliseconds < 40000)
                {
                    continue;
                }
                cacheItem = GetCacheRandom(cache);
                Console.Write("40s后取值    ");
                ShowCacheItem(cacheItem);
                break;
            }

            while (true)
            {
                if (watch.ElapsedMilliseconds < 70000)
                {
                    continue;
                }
                cacheItem = GetCacheRandom(cache);
                Console.Write("70s后取值,此时应该是新值    ");
                ShowCacheItem(cacheItem);
                break;
            }
            
            watch.Stop();

            Console.WriteLine("输入任意键退出!");
            Console.ReadKey();

        }

    }
}
